/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 21:14:38
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.api.mh.repository;

import com.joysuch.wwyt.api.mh.entity.MonitorPointAPIData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;


public interface MonitorPointDataAPIRepository extends JpaRepository<MonitorPointAPIData, Long> {


    @Query(value = "select * from wwyt_api_mh_monitor_point_data m where tenent_id = ?1 and org_code=?2 and tag_time >=?3 and tag_value !=-9999 order by tag_time desc ", nativeQuery = true)
    List<MonitorPointAPIData> findAllByTenentIdAndOrgCode(Long tenentId, String orgCode, Date startDate);

    @Query(value = "SELECT * FROM `wwyt_api_mh_monitor_point_data` AS a WHERE a.tag_name IN (SELECT b.point_code FROM `wwyt_mh_platform_point` AS b WHERE \n" +
            "b.delete_flag ='0' AND b.switch_state = 1 AND b.platform_id =?1 AND a.tag_name = b.point_code) AND a.tag_time BETWEEN ?2 AND ?3", nativeQuery = true)
    List<MonitorPointAPIData> getRealTimeData(Long platformId, Date date, Date now);

    @Query(value = "SELECT * FROM `wwyt_api_mh_monitor_point_data` AS a WHERE a.tag_name in :tagName and a.tag_time between :start and :end", nativeQuery = true)
    List<MonitorPointAPIData> findByTagNameInAndTagTimeBetween(List<String> tagName, Date start, Date end);

    @Query(value = "SELECT * FROM `wwyt_api_mh_monitor_point_data` where tag_name = ?1 and tenent_id = ?2 and org_code = ?3 order by tag_id desc limit 1", nativeQuery = true)
    MonitorPointAPIData findOneByTagName(String tagName,Long tenantId, String orgcode);
}
